package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.model.cmn.Dict;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.atguigu.yygh.cmn.service.DictService;
import org.springframework.web.bind.annotation.*;
import com.atguigu.yygh.common.result.Result;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author lijing
 * @since 2025-04-28
 */
@Api("数据字典管理")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Autowired
    private DictService dictService;
    @ApiOperation("获取资源子元素")
    @GetMapping("findChildData/{id}")
    public Result getList(@PathVariable("id")Long id) {
        List<Dict> list = dictService.queryList(id);
        return Result.ok(list);
    }

    @ApiOperation("导出")
    @GetMapping("/exportData")
    public Result exportData(HttpServletResponse response) {
        dictService.exportData(response);
        return Result.ok();
    }

    @ApiOperation("导出")
    @PostMapping("/importData")
    public Result importData(MultipartFile file){
        dictService.importData(file);
        return Result.ok();
    }

//
//    @PostMapping("/getList")
//    public Result getList(@RequestBody Dict dict) {
//        IPage<Dict> page = dictService.selectDictList(dict);
//        return Result.ok(page);
//    }
//
//    @DeleteMapping("/{id}")
//    public Result deleteByID(Long id) {
//        return dictService.deleteDict(id);
//    }
//
//    @PostMapping("/saveModel")
//    public Result insert(@RequestBody Dict dict) {
//        return dictService.insertDict(dict);
//
//    }
//
//    @PutMapping("/updateModel")
//    public Result update(@RequestBody Dict dict) {
//        return dictService.updateDict(dict);
//    }

    //获取字典值

    @GetMapping("/getName/{value}/{dictCode}")
    public String getNameByValueAndDictCode(@PathVariable("value") String value,  @PathVariable(value = "dictCode",required = false) String dictCode) {
        String dictName = dictService.getDictName(value,dictCode);
        return dictName;
    }

    @GetMapping ("/getNameByValue/{value}")
    public String getNameByValue(@PathVariable("value") String value) {
        String dictName = dictService.getDictNameByValue(value);
        return dictName;
    }


    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable("dictCode") String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }


    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findChilderData/{id}")
    public Result<List<Dict>> findChilderData(
            @ApiParam(name = "id", value = "节点编码", required = true)
            @PathVariable("id") Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }


}

